package com.shuidi.oilactivity.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.shuidi.oilactivity.MainActivity;
import com.shuidi.oilactivity.R;
import com.shuidi.oilactivity.bean.Person;

import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;

public class LandActivity extends BaseActivity {

    private EditText account;
    private EditText password;
    private Button go;
    private int roleId;
    private boolean stye = false;
    private SharedPreferences sharedPreferences;
    private int goId = 0;
    private String objectId;
    private int userId;
    private String name;
    private String iphone;
    private String vehicleNumber;
    private String companyName;
    private String companyAddress;
    private String vehiclepassword;
    private int sex;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_land);
        init();
    }

    private void init(){
        account = (EditText)findViewById(R.id.account);
        password = (EditText)findViewById(R.id.password);
        go = (Button)findViewById(R.id.go);

        go.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (account.getText().toString().length() == 0){
                    Toast.makeText(LandActivity.this, "请输入您的账号", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (password.getText().toString().length() == 0){
                    Toast.makeText(LandActivity.this, "请输入您的账号", Toast.LENGTH_SHORT).show();
                    return;
                }
                find();
            }
        });
    }

    private void find(){
        BmobQuery<Person> query = new BmobQuery<Person>();

        query.addWhereEqualTo("name", account.getText()+"");
        //执行查询方法
        query.findObjects(new FindListener<Person>() {
            @Override
            public void done(List<Person> object, BmobException e) {
                if(e==null){
                    //toast("查询成功：共"+object.size()+"条数据。");
                    for (Person person : object) {
                        if (person.getPassword().equals(password.getText()+"") && (person.getRoleId() == 0 || person.getRoleId() ==1)){
                            stye = true;
                            //获得RoleId的信息
                            roleId = person.getRoleId();
                            objectId = person.getObjectId();
                            userId = person.getUserId();
                            name = person.getName();
                            iphone = person.getIphone();
                            companyName = person.getCompanyName();
                            companyAddress = person.getCompanyAddress();
                            vehicleNumber = person.getVehicleNumber();
                            vehiclepassword = person.getVehiclepassword();
                            sex = person.getSex();
                        }
                    }

                    if (stye){
                        Intent intent = new Intent(LandActivity.this,MainActivity.class);
                        intent.putExtra("roleId",roleId+"");
                        startActivity(intent);
                        clearData();
                        saved(objectId,userId,name,iphone,companyName,companyAddress,vehicleNumber,vehiclepassword);
                        finish();

                    }else {
                        Toast.makeText(LandActivity.this, "账号或密码不对，若未开通，请联系管理员", Toast.LENGTH_SHORT).show();
                    }

                }else{
                    Log.i("bmob","失败："+e.getMessage()+","+e.getErrorCode());
                    Toast.makeText(LandActivity.this, "登陆失败，请查看网络", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    private void saved(String objectId,int userId,String name,String iphone, String companyName, String companyAddress, String vehicleNumber,String vehiclepassword){
        SharedPreferences.Editor editor= getSharedPreferences("userData",MODE_PRIVATE).edit();
        editor.putString("vehicleNumber",vehicleNumber);
        editor.putString("vehiclepassword",vehiclepassword);
        editor.putString("objectId",objectId);
        editor.putInt("userId",userId);
        editor.putInt("sex",sex);
        editor.putInt("roleId",roleId);
        editor.putString("name",name);
        editor.putString("iphone",iphone);
        editor.putString("companyName",companyName);
        editor.putString("companyAddress",companyAddress);
        editor.commit();
    }
    private void clearData(){
        SharedPreferences.Editor editor= getSharedPreferences("userData",MODE_PRIVATE).edit();
        editor.clear();
        editor.commit();

    }
}
